<?php
namespace Home\Controller;
use Admin\Model\NewscateModel;
use Admin\Model\NewsModel;
use Admin\Model\OrderModel;
use Admin\Model\PhotocateModel;
use Admin\Model\PhotoModel;
use Common\Model\CommonModel;
use Think\Controller;


class IndexController extends Controller
{

    public function index()
    {
        $photocateModel=new PhotocateModel();
        $photoModel=new PhotoModel();
        //banner 图片
        $data['name']='banner';
        $photo_cate=$photocateModel->where($data)->find()['id'];
        if(!empty($photo_cate)){
            $arrPhoto=$photoModel->where('photo_cate='.$photo_cate)->field('pic')->select();
            $arrPhoto=CommonModel::photo1($arrPhoto);
            $this->assign('arrPhoto',$arrPhoto);
        }
        //预约人数
        $orderModel=new OrderModel();
        $count=$orderModel->count()*20;
        $this->assign('count',$count);
        //轮播展示图 >=5张
        $dat['name']='轮播图';
        $photo_cate=$photocateModel->where($dat)->find()['id'];
        if(!empty($photo_cate)){
            $arrPhoto1=$photoModel->where('photo_cate='.$photo_cate)->field('pic,name')->select();
            $arrPhoto1=CommonModel::photo1($arrPhoto1);
            $this->assign('arrPhoto1',$arrPhoto1);
        }
        //装修风格图
        $da['name']='装修风格';
        $photo_cate=$photocateModel->where($da)->find()['id'];
        if(!empty($photo_cate)){
            $arrPhoto2=$photoModel->where('photo_cate='.$photo_cate)->field('pic,name,remark')->limit(8)->select();
            $i=1;
            foreach($arrPhoto2 as $key=>$value){
                $arrPhoto2[$key]['id']=$arrPhoto2[$key]['id']+$i;
                ++$i;
            }
            $arrPhoto2=CommonModel::photo1($arrPhoto2);
            $this->assign('arrPhoto2',$arrPhoto2);
        }

        //集团介绍
        $newsModel=new NewsModel();
        $newscateModel=new NewscateModel();
        $data1['name']='集团介绍';
        $news_cate=$newscateModel->where($data1)->find()['id'];
        if(!empty($news_cate)){
            $arrNews=$newsModel->where('news_cate='.$news_cate)->field('title,abstract,layout as pic')->limit(1)->select();
            $arrNews=CommonModel::photo1($arrNews);
            $this->assign('arrNews',$arrNews);
        }

        //公司动态
        $data2['name']='公司动态';
        $news_cate=$newscateModel->where($data2)->find()['id'];
        if(!empty($news_cate)){
            $arrNews1=$newsModel->where('news_cate='.$news_cate)->field('title,abstract,create')->order('`create` desc')->limit(5)->select();
            $arrNews1[0]['day']=date('d',$arrNews1[0]['create']);
            $arrNews1[0]['month']=date('Y-m',$arrNews1[0]['create']);
            $first=$arrNews1[0];
            if(!empty($first)){
                $this->assign('first',$first);
            }
            unset($arrNews1[0]);
            $arrNews1=CommonModel::status1($arrNews1);
            $this->assign('arrNews1',$arrNews1);
        }
        //装修课堂
        $data3['name']='装修课堂';
        $news_cate=$newscateModel->where($data3)->find()['id'];
        if(!empty($news_cate)){
            $arrNews2=$newsModel->where('news_cate='.$news_cate)->field('title,abstract,layout as pic')->limit(6)->select();
            $arrNews2=CommonModel::photo1($arrNews2);
            $this->assign('arrNews2',$arrNews2);
        }
        //右侧装修课堂
        $data4['name']='装修课堂2';
        $news_cate=$newscateModel->where($data4)->find()['id'];
        if(!empty($news_cate)) {
            $arrNews3 = $newsModel->where('news_cate=' . $news_cate)->field('title,abstract')->limit(2)->select();
            $arrNews3 = CommonModel::photo1($arrNews3);
            $this->assign('arrNews3', $arrNews3);
        }
        //区域
        $areaModel=M('area');
        $arrArea=$areaModel->select();
        $this->assign('arrArea',$arrArea);
        //var_dump($arrNews1);exit;
        $this->display();
    }

    public function order()
    {
        $data['status']=htmlspecialchars($_POST['status']);
        $data['name']=htmlspecialchars($_POST['name']);
        $data['mobile']=htmlspecialchars($_POST['mobile']);
        $orderModel=M('order');
        $arrOrder=$orderModel->where("mobile=".$data['mobile'])->find();
        if(!empty($arrOrder)){
            echo 3;
            exit;
        }else{
            $data['create']=time();
            $res=$orderModel->add($data);
            if($res){
                echo 1;
            }else{
                echo 2;
            }
        }
    }


    public function message()
    {
        /*
        ***聚合数据（JUHE.CN）短信API服务接口PHP请求示例源码
        ***DATE:2015-05-25
    */
        $yzmModel = D('yzm');
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL

        //$ip=getenv('REMOTE_ADDR');
        //echo $this->tpl;exit;
        $mobile = htmlspecialchars($_POST['mobile']);
        $yzmArr = array();
        $yzmArr = $yzmModel->finded('mobile=' . $mobile);
        //echo $yzmArr['mobile'];exit;
        //判断该手机号当日是否已发送两次
        if (strtotime(date('Y-m-d', time())) <= $yzmArr['create'] && $yzmArr['create'] <= strtotime(date('Y-m-d H:i:s', time()))) {
            if ($yzmArr['count'] >= 2) {
                echo "验证码已发送，请耐心等待";
                exit;
            }
        } else {
            $yzmArr['count'] = 0;
        }
        if (empty($yzmArr['number'])) {
            $yzmArr['number'] = rand(10000, 99999);
        }

        $smsConf = array(
            'key' => 'e65a1b7f6e8a9fc5f1ddb2ae283d3af8', //您申请的APPKEY
            'mobile' => $mobile, //接受短信的用户手机号码
            'tpl_id' => '23721', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#=' . $yzmArr['number'] //您设置的模板变量，根据实际情况修改
        );
        $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
        if ($content) {
            $result = json_decode($content, true);
            $error_code = $result['error_code'];
            if ($error_code == 0) {
                //状态为0，说明短信发送成功
                $yzmArr['create'] = time();
                if($yzmArr['count']){
                    $yzmArr['count'] = $yzmArr['count'] + 1;
                    $res=$yzmModel->update($mobile, $yzmArr);
                }else{
                    $yzmArr['count']=1;
                    $yzmArr['mobile']=$mobile;
                    $res=$yzmModel->add($yzmArr);
                }
                if($res){
                    echo "短信发送成功";
                }else{
                    echo "出现异常";
                }

            } else {
                //状态非0，说明失败
                $msg = $result['reason'];
                echo "短信发送失败";
            }
        } else {
            //返回内容异常，以下可根据业务逻辑自行修改
            echo "请求发送短信失败";
        }
    }

    public function confirm()
    {
        $data['number'] = htmlspecialchars($_POST['number']);
        $data['mobile'] = htmlspecialchars($_POST['mobile']);
        $yzmModel = D('yzm');
        $yzmArr = $yzmModel->finded("number=" . $data['number'] . " and  mobile=" . $data['mobile']);

        //echo $yzmArr['mobile']; exit;
        if (!empty($yzmArr)) {
            $fastModel = D('fast');
            $list['mobile'] = htmlspecialchars($_POST['mobile']);
            $res = $fastModel->where(" mobile= " . $list['mobile'])->find();

            if (empty($res)) {
                $list['name'] = htmlspecialchars($_POST['name']);
                $list['place'] = htmlspecialchars($_POST['place']);
                $list['status'] = htmlspecialchars($_POST['status']);
                $list['create'] = time();
                $resu = $fastModel->add($list);
                if ($resu) {
                    echo 1;
                    exit;
                }
            }
            echo '您已获取报价';
            exit;
        }
        echo '验证码有误';
        exit;
    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juhecurl($url, $params = false, $ispost = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }


    public function fix(){
        $data['name']=htmlspecialchars($_POST['name']);
        $data['mobile']=htmlspecialchars($_POST['mobile']);
        $data['city']='上海';
        $data['area_id']=htmlspecialchars($_POST['area']);
        $list=M('fast1')->where('mobile='.$data['mobile'])->find();
        if(!empty($list)){
            echo 2;
            exit;
        }else{
            $data['create']=time();
            $res=M('fast1')->add($data);
            if($res){
                echo 1;
                exit;
            }
        }

    }

}